﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using YourWorld.Common.InitializeServerServiceReference;
using System.ServiceModel;
using System.ComponentModel;
using System.Threading;
using YourWorld.Common.Infrastucture.Events;

namespace YourWorld.Common
{
    public class InitializeServerManager
    {
        private static InitializeServerManager instance;

        public static bool IsInitialized = false;
        public static bool IsInicializationRequested = false;
        private static string ServcieName = "InitializeServersService.svc";

        private string serviceBaseUrl;

        public Action InitializationCompleted
        {
            get;
            set;
        }

        private InitializeServerManager()
        {
            EventService.Aggregator.GetEvent<ServicesLocationEvent>().Subscribe( this.ServicesLocationInitialized );
        }

        public static InitializeServerManager Instance
        {
            get
            {
                if ( instance == null )
                    instance = new InitializeServerManager();

                return instance;
            }
        }

        public void ServicesLocationInitialized( string url )
        {
            this.serviceBaseUrl = url;
        }

        public void Initialize()
        {
            //IsInicializationRequested = true;
            //string url = String.Format( "{0}{1}", this.serviceBaseUrl, ServcieName );

            //InitializeServersServiceClient client = new InitializeServersServiceClient(
            //    new BasicHttpBinding(),
            //    new EndpointAddress( url ) );

            //client.InitializeCompleted += new EventHandler<AsyncCompletedEventArgs>( this.Client_InitializeCompleted );
            //client.InitializeAsync();
        }

        private void Client_InitializeCompleted( object sender, AsyncCompletedEventArgs e )
        {
            IsInitialized = true;
            IsInicializationRequested = false;
        }
    }
}
